package com.zlll.winner.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

@Slf4j
public class DateUtils {

    /** 日期格式yyyy-MM-dd HH:mm:ss字符串常量 */
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";



    /**
     * 自定义格式化日期,
     * @param format
     * @param date
     * @return
     */
    public static String formatDateTime(String  format, Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            return simpleDateFormat.format(date.getTime());
        } catch (Exception e) {
            log.error("{}",e);
            return "";
        }
    }

    public static String formatLongTime(String format, Long date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date(date));
    }

    public static Date getDateByString(String pattern, String time) {
        Date date = null;
        if(time == null) {
            return date;
        }

        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            date = format.parse(time);
        } catch (Exception e) {
            log.error("{}",e);
        }
        return date;
    }

    /**
     * 显示当前时间距离发布时间的时间间隔,
     * @param time
     * @return
     */
    public static String getShortTime(String time) {
        String shortString;
        long now = Calendar.getInstance().getTimeInMillis();
        Date date = getDateByString("yyyy-MM-dd HH:mm:ss",time);
        if(date == null) {
            return null;
        }
        long delTime = (now - date.getTime()) / 1000;
        if (delTime > 365 * 24 * 60 * 60) {
            shortString = (int) (delTime / (365 * 24 * 60 * 60)) + "年前";
        } else if (delTime > 24 * 60 * 60) {
            shortString = (int) (delTime / (24 * 60 * 60)) + "天前";
        } else if (delTime > 60 * 60) {
            shortString = (int) (delTime / (60 * 60)) + "小时前";
        } else if (delTime > 60) {
            shortString = (int) (delTime / (60)) + "分前";
        } else if (delTime > 1) {
            shortString = delTime + "秒前";
        } else {
            shortString = "1秒前";
        }
        return shortString;
    }

    public static String toDay(){
        String day = DateUtils.formatDateTime("yyyy-MM-dd",new Date(System.currentTimeMillis()));
        return day;
    }

    /**
     * 获取当天剩余秒数
     * @param currentDate
     * @return
     */
    public static long getRemainSecondsOneDay(Date currentDate){
        LocalDateTime midnight = LocalDateTime.ofInstant(currentDate.toInstant(),
                ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0)
                .withSecond(0).withNano(0);
        LocalDateTime currentDateTime = LocalDateTime.ofInstant(currentDate.toInstant(),
                ZoneId.systemDefault());
        long seconds = ChronoUnit.SECONDS.between(currentDateTime, midnight);
        System.out.println(seconds);
        return seconds;
    }

    /**
     * 转换返回的显示时间,
     */
    public static String dateFormatToString(Long time,String formate) {
        Assert.notNull(time, "time is null");
        Long now = System.currentTimeMillis();
        DateTimeFormatter day = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String orgDay = day.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
        String nowDay = day.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(now), ZoneId.systemDefault()));
        if (orgDay.equals(nowDay)) {
            //同一天,判断多少时间前
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            return getShortTime(dtf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault())));
        }else {
            //不同天返回指定格式
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(formate);
            return dtf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
        }
    }
}
